package com.software.work;


import com.software.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Run {
    public static void main(String[] args) {
        Run run = new Run();

        run.save();
        //根据专业ID，查询专业，并查询专业下的所有学生。
//        run.get();
//        //删除专业时级联删除学生信息。
//        run.delete();
////        run.save1();
//        //实现查询学生时，可查看到此学生所选的所有的课程。
//        run.getSutdent();
//        //查看课程时，可以查看选择此课程的所有学生。
//        run.getCourse();


    }

    //查看课程时，可以查看选择此课程的所有学生。
    private void getCourse() {

        Session session = HibernateUtils.getSession();

        Course course = session.get(Course.class,"1");
        if (course == null){
            System.out.println("null");
            return;
        }
        for(Student student : course.getStudents()){
            System.out.println(student.toString());
        }


        session.close();
    }

    //实现查询学生时，可查看到此学生所选的所有的课程。
    private void getSutdent() {

        Session session = HibernateUtils.getSession();

        Student student = session.get(Student.class, "2021012412");
        if (student == null){
            System.out.println("null");
            return;
        }
        System.out.println(student.getStudentName());

        for(Course course : student.getCourses()){
            System.out.println(course.toString());
        }

        session.close();

    }

    //删除专业时级联删除学生信息。
    private void delete() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();

        Major major = session.get(Major.class,"001");
        session.delete(major);

        transaction.commit();
        session.close();

    }

    //根据专业ID，查询专业，并查询专业下的所有学生。
    private void get() {

        Session session = HibernateUtils.getSession();

        Major major = session.get(Major.class, "001");
        if (major == null){
            System.out.println("null");
            return;
        }
        System.out.println(major.getMajorId());

        Set<Student> students = major.getStudents();
        for (Student student : students) {
            System.out.println(student.toString());
        }

        session.close();



    }

    private void save1() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();

        Student s1 = new Student();
        Student s2 = new Student();
        s1.setStudentName("张三");
        s1.setStudentNo("2021012412");
        s1.setTelephone("12345678978");
        s1.setSex("男");
        s2.setStudentName("李四");
        s2.setStudentNo("2021012413");
        s2.setTelephone("12345678989");
        s2.setSex("女");

        Course c1 = new Course();
        Course c2 = new Course();
        c1.setCourseName("java");
        c1.setCourseId("001");
        c1.setCourseCredit(4.0);
        c2.setCourseName("python");
        c2.setCourseId("002");
        c2.setCourseCredit(3.0);

        s1.getCourses().add(c1);
        s1.getCourses().add(c2);
        s2.getCourses().add(c1);
        s2.getCourses().add(c2);

        c1.getStudents().add(s1);
        c1.getStudents().add(s2);
        c2.getStudents().add(s1);
        c2.getStudents().add(s2);


        session.save(s1);
        session.save(s2);
        session.save(c1);
        session.save(c2);

        transaction.commit();
        session.close();
    }

    private void save() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();

        Major major = new Major();
        major.setMajorId("001");
        major.setMajorName("软件工程");
        major.setMajorDetail("软件工程");

        Student s1 = new Student("2021012411","15613313389","jzh","M");
        Student s2 = new Student("2021012412","15613313377","zas","F");
        Student s3 = new Student("2021012413","15613313378","zys","M");

        major.getStudents().add(s1);
        major.getStudents().add(s2);
        major.getStudents().add(s3);

        session.save(major);

        transaction.commit();
        session.close();
    }



}
